package com.xiaohao.server.parkmap.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaohao.server.parkmap.entity.TreeAdoption;
import com.xiaohao.server.parkmap.mapper.TreeAdoptionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;

@Service
public class TreeAdoptionService {
    @Autowired
    private TreeAdoptionMapper treeAdoptionMapper;

    public TreeAdoption queryById(Integer id) {
        return treeAdoptionMapper.selectById(id);
    }

    public TreeAdoption queryOne(TreeAdoption treeAdoption) {
        return treeAdoptionMapper.selectOne(new QueryWrapper<>(treeAdoption));
    }

    public List<TreeAdoption> queryList(TreeAdoption treeAdoption) {
        LambdaQueryWrapper<TreeAdoption> queryWrapper =
            new LambdaQueryWrapper<>(treeAdoption);
        return treeAdoptionMapper.selectList(queryWrapper);
    }

    public Integer delete(Integer id) {
        return treeAdoptionMapper.deleteById(id);
    }

    public Integer save(TreeAdoption treeAdoption) {
        if (Objects.nonNull(treeAdoption) && Objects.nonNull(treeAdoption.getId())) {
            return treeAdoptionMapper.updateById(treeAdoption);
        } else {
            treeAdoption.setDate(new Timestamp(System.currentTimeMillis()));
            return treeAdoptionMapper.insert(treeAdoption);
        }
    }
}
